
package com.example.demo.utils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public final class JsonUtil {
    private static final ObjectMapper mapper = new ObjectMapper();

    private JsonUtil() {
    }

    public static String serializeOrNull(Object object) {
        if (object == null) {
            return "";
        } else {
            try {
                return mapper.writeValueAsString(object);
            } catch (JsonProcessingException var2) {
                throw new RuntimeException(var2);
            }
        }
    }

    public static String serialize(Object object) {
        if (object == null) {
            throw new NullPointerException();
        } else {
            try {
                return mapper.writeValueAsString(object);
            } catch (JsonProcessingException var2) {
                throw new RuntimeException(var2);
            }
        }
    }

    public static <T> T deserialize(String jsonString, Class<T> classType) {
        if (jsonString != null && classType != null) {
            try {
                return mapper.readValue(jsonString, classType);
            } catch (JsonProcessingException var3) {
                throw new RuntimeException(var3);
            }
        } else {
            throw new NullPointerException();
        }
    }

    public static <T> T deserializeOrNull(String jsonString, Class<T> classType) {
        if (jsonString != null && classType != null) {
            try {
                return mapper.readValue(jsonString, classType);
            } catch (JsonProcessingException var3) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static <T> T deserialize(String jsonString, TypeReference<T> typeReference) {
        if (jsonString != null && typeReference != null) {
            try {
                return mapper.readValue(jsonString, typeReference);
            } catch (JsonProcessingException var3) {
                throw new RuntimeException(var3);
            }
        } else {
            throw new NullPointerException();
        }
    }


    public static <T> T convert(Object obj, Class<T> type) {
        return obj == null ? null : mapper.convertValue(obj, type);
    }


    static {
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        mapper.setSerializationInclusion(Include.NON_NULL);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    }
}
